<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class GuessStarController extends MessageController {


    /**
     * 缓存到文件
     */
    private function cachestar(){

        $info = M('star_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id,star_member_id')->where(array('status'=>'-1'))->order('wxopen_template_send_id asc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('cachestar',$data,7200);

    }


    /**
     * 猜明星
     * @return int
     */
    public function sendstar(){

        exit;
//        $wheret['touser'] = "o1ZgM5Lh4b6pVWmCAcd-GV5qYoF0";
//        $wheret['status'] = "-1";
//        $info = M('star_template_send')->field('wxopen_template_send_id,appid,secret,touser,template_id,page,form_id')->where($wheret)->limit(1)->order('wxopen_template_send_id desc')->select();

        $info = S('cachestar');

        if (empty($info)){
            $this->cachestar();
            $info = S('cachestar');
        }


        foreach ($info as $key=>$value){


            $fdata = array(
                'keyword1'=>array(
                    'value'=>date('Y-m-d'),
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>'范冰冰说她没空',
                    'color'=>'#ff0000',
                ),
                'keyword3'=>array(
                    'value'=>'她说最近很烦',
                    'color'=>'#173177',
                ),

            );
            $tem = "K21rIUZuOoy_uz7jUInB9KAarQEdvlZS8XkzomYQ_Lg";
            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$tem,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword2.DATA'
            );
            $res = $this->sendtemplate($value['appid'],$value['secret'],$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($fdata),
                'emphasis_keyword'=>'keyword1.DATA',
            );
            echo $res['errcode'];
            if ($res['errcode']==0){
                $save['status'] = 1;
                $save['send_time'] = time();
                M('star_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('star_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }


}